<template>
    <div class="add-drug-form">
        <el-form :model="drugForm" status-icon :rules="rules" ref="drugForm" label-width="100px">
            <el-form-item label="药品名称：" prop="name">
                <el-input v-model="drugForm.name"></el-input>
            </el-form-item>
            <el-form-item label="药品类型：" prop="type">
                <el-input v-model="drugForm.type"></el-input>
            </el-form-item>
            <el-form-item label="药品价格：" prop="price">
                <el-input v-model="drugForm.price"></el-input>
            </el-form-item>
            <el-form-item label="药品图片：" prop="url">
                <el-input v-model="drugForm.url"></el-input>
            </el-form-item>
            <el-form-item label="药品描述：" prop="description">
                <el-input v-model="drugForm.description"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm">提交</el-button>
                <el-button @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script type="text/ecmascript-6">
    export default {
        data() {
            let validateName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('药品名称不能为空'));
                } else {
                    callback();
                }
            };
            let validateType = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('药品编号不能为空'));
                } else {
                    callback();
                }
            };
            let validateParentType = (rule, value, callback) => {
                if (value === '') {
                    return callback(new Error('药品价格'));
                } else {
                    callback();
                }
            };
            return {
                drugForm: {
                    name: '',
                    type: '',
                    parentType: ''
                },
                rules: {
                    name: [
                        {validator: validateName, trigger: 'blur'}
                    ],
                    type: [
                        {validator: validateType, trigger: 'blur'}
                    ],
                    parentType: [
                        {validator: validateParentType, trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            submitForm() {
                this.$refs["drugForm"].validate((valid) => {
                    if (valid) {
                        this.$http.get("/category/save", {
                            params: {
                                categoryName: this.drugForm.name,
                                categoryType: this.drugForm.type,
                                categoryParentType: this.drugForm.parentType
                            }
                        }).then((response) => {
                            response = response.data;
                            if (response.code === 0) {
                                this.$message({
                                    message: response.msg,
                                    type: 'success',
                                    customClass: 'alert-shadow'
                                });
                            } else {
                                this.$message({
                                    message: response.msg,
                                    type: 'error',
                                    customClass: 'alert-shadow'
                                });
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            resetForm() {
                this.$refs["drugForm"].resetFields();
            }
        }
    }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>

</style>
